package com.ssy.lingxi.contract.esign;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.api.SignApi;
import com.ssy.lingxi.contract.esign.request.FaceCheckAuthRequest;
import com.ssy.lingxi.contract.esign.request.OrgCheck3AuthRequest;
import com.ssy.lingxi.contract.esign.request.TelecomCheckAuthRequest;
import com.ssy.lingxi.contract.esign.request.TransferRandomAmountRequest;
import com.ssy.lingxi.contract.esign.response.FaceAuthResponse;
import com.ssy.lingxi.contract.esign.response.OrgAuthResponse;
import com.ssy.lingxi.contract.esign.response.SubbranchDetailResponse;
import com.ssy.lingxi.contract.esign.response.TelecomAuthResponse;
import com.ssy.lingxi.contract.esign.util.ApiUtil;

import java.math.BigDecimal;

public class AuthDemo {

    /**
     * 项目(网关)地址
     */
    String projectUrl = "";
    SignApi signApi = null;

    /**
     * 发起企业核身认证3要素校验
     */
    public void test1() throws Exception {

        OrgCheck3AuthRequest request = OrgCheck3AuthRequest.builder()
                .name("广州市数商云网络科技有限公司")
                .orgCode("91440105065805264E")
                .legalRepName("谢建松")
                .contextId("f0a7927d-c5f9-4652-a053-1130d86c8fa8")
                .notifyUrl(projectUrl + "/order/esignNotify/organizationAuth")
                .build();
        OrgAuthResponse response = signApi.enterpriseAuth().check3Auth(request);

        // {"flowId":"1411433214391617315"}
        System.out.println(JSONUtil.toJsonStr(response));
    }

    // =============================银行打款=============================

    /**
     * 查询打款银行信息
     */
    public void test2() throws Exception {
        String flowId = "1411433214391617315";
        String keyword = "中国工商银行广州粤秀支行";

        SubbranchDetailResponse response = signApi.enterpriseAuth().subbranchDetail(flowId, keyword);

        for (SubbranchDetailResponse.BankDetail bankDetail : response.getList()) {
            if ("中国工商银行广州粤秀支行".equals(bankDetail.getBankName())) {

            }
        }

        // {"list":[{"city":"广州市","bankName":"中国工商银行股份有限公司广州粤秀支行","cnapsCode":"102581000249","bank":"中国工商银行","province":"广东"},{"city":"广州市","bankName":"中国工商银行股份有限公司广州粤电支行","cnapsCode":"102581003740","bank":"中国工商银行","province":"广东"},{"city":"广州市","bankName":"中国工商银行股份有限公司广州越秀桥支行","cnapsCode":"102581000659","bank":"中国工商银行","province":"广东"},{"city":"广州市","bankName":"广东南粤银行股份有限公司广州粤垦路支行","cnapsCode":"313581096050","bank":"广东南粤银行","province":"广东"},{"city":"泉州市","bankName":"中国工商银行泉州泉秀支行","cnapsCode":"102397021043","bank":"中国工商银行","province":"福建"},{"city":"福州市","bankName":"中国工商银行福州锦秀支行","cnapsCode":"102391052351","bank":"中国工商银行","province":"福建"},{"city":"广州市","bankName":"中国银行股份有限公司广州粤电支行","cnapsCode":"104581006218","bank":"中国银行","province":"广东"},{"city":"广州市","bankName":"中国银行股份有限公司广州粤垦路支行","cnapsCode":"104581006259","bank":"中国银行","province":"广东"},{"city":"广州市","bankName":"南洋商业银行（中国）有限公司广州越秀支行","cnapsCode":"503581000043","bank":"南洋商业银行","province":"广东"},{"city":"广州市","bankName":"广东南粤银行股份有限公司广州分行","cnapsCode":"313581096009","bank":"广东南粤银行","province":"广东"}]}
        System.out.println(JSONUtil.toJsonStr(response));
    }

    /**
     * 发起随机金额打款认证
     */
    public void test3() throws Exception {
        TransferRandomAmountRequest request = TransferRandomAmountRequest.builder()
                .flowId("1411433214391617315")
                .province("广东")
                .subbranch("中国工商银行股份有限公司广州粤秀支行")
                .bank("工商银行")
                .city("广州市")
                .cnapsCode("102581000249")
                .cardNo("3602015009201436525")
                .notifyUrl(projectUrl + "/order/esignNotify/transferRandomAmount")
                .build();

        signApi.enterpriseAuth().transferRandomAmount(request);
    }

    /**
     * 随机金额校验
     */
    public void test4() throws Exception {
        String flowId = "1411433214391617315";
        BigDecimal amount = new BigDecimal("0.01");

        signApi.enterpriseAuth().verifyRandomAmount(flowId, amount);
    }

    // =============================人脸核身认证=============================

    /**
     * 发起个人刷脸核身认证
     */
    public void test5() throws Exception {


        FaceCheckAuthRequest request = FaceCheckAuthRequest.builder()
                .name("姚梓聪")
                .idNo("441322199610065214")
                .faceauthMode("ZHIMACREDIT")
                .callbackUrl(projectUrl)
                .notifyUrl(projectUrl + "/order/esignNotify/faceAuth")
                .build();

        FaceAuthResponse response = signApi.personalAuth().faceCheckAuth(request);

        // {"originalUrl":"https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-3.4.107.ALL&app_id=2019021563241544&biz_content=%7B%22certify_id%22%3A%22cc5531b3d59f7bc6929b540f5a995596%22%7D&charset=GBK&format=json&method=alipay.user.certify.open.certify&sign=on8ccv%2FZkte6E3N2N5tOL4NlvRpyw7D7GBGMYaR8IceWkWrzjgpe%2FHC4PXk4lmcN7nNFJk8fuchqchs%2FAe4AU0U1QQlTgViiVPP3GibkQm9Q64ntF5EGzpgUzrb0VR5akyCpUXLTtWWwcJOnrVCQx23lBxSJceWEuDggqvlCobtCBV7XXsA6Mq0JutlkKcWby82ATPl2KdVJ7yTEulkvAsZ%2BanjPn5e4Nby832kVVWHioy73b0zMjjfl%2Fa1lynvGyLPgxVTE8aZeoyU8eTZA7YMqGlhiALjwSAcsiQyEb2ARL3GvX3faqxRS7Mzz8hYH%2F3qMfAn1RRShZiCMTW9iog%3D%3D&sign_type=RSA2&timestamp=2020-08-31+20%3A21%3A16&version=1.0","authUrl":"https://smlt.tsign.cn/8SEygbUnWhRQ","expire":1598876596946,"flowId":"1411642943567038149"}
        System.out.println(JSONUtil.toJsonStr(response));
    }


    // =============================手机认证=============================

    /**
     * 发起运营商3要素核身认证
     */
    public void test6() throws Exception {

        TelecomCheckAuthRequest request = TelecomCheckAuthRequest.builder()
                .name("姚梓聪")
                .idNo("441322199610065214")
                .mobileNo("13250151294")
                .notifyUrl(projectUrl + "/order/esignNotify/telecomAuth")
                .build();

        TelecomAuthResponse response = signApi.personalAuth().telecomCheckAuth(request);

        // {"code":0,"message":"成功","data":{"flowId":"1412516304664921873"}}
        System.out.println(JSONUtil.toJsonStr(response));
    }

    /**
     * 运营商短信验证码校验
     */
    public void test7() throws Exception {


        String flowId = "1412516304664921873";
        String authcode = "588058";

        signApi.personalAuth().telecomAuthCode(flowId, authcode);
    }


    public void verifySign() throws Exception {
        String headerStr = "{\"content-length\":\"200\",\"x-tsign-open-signature-algorithm\":\"hmac-sha256\",\"x-tsign-open-timestamp\":\"1598866500779\",\"x-tsign-open-app-id\":\"7438817714\",\"sw6\":\"1-MzkwNzQuMzQwLjE1OTg4NjY1MDA3ODExMjM5-MzkwNzQuMzQwLjE1OTg4NjY1MDA3ODExMjM4-0-39074-39074-I3l6Yy5uZ3JvazIueGlhb21pcWl1LmNuOjQ0Mw==-LTE=-LTE=\",\"cat\":\"39074T340T15988665007811238|0|172.24.85.99:msg-push-center||#yzc.ngrok2.xiaomiqiu.cn:443|-1|-1|39074T340T15988665007811239\",\"host\":\"yzc.ngrok2.xiaomiqiu.cn\",\"content-type\":\"application/json; charset=utf-8\",\"connection\":\"Keep-Alive\",\"x-tsign-open-signature\":\"58951af8405ca5bcd4c42e9f536c092b0e7fb3de34fc639395da2e3a07c7eb19\",\"accept-encoding\":\"gzip\",\"user-agent\":\"okhttp/3.7.0\"}";
        String bodyStr = "{\"msg\":\"转账交易成功\",\"result\":\"PAY_SUCCESS\",\"payedTime\":1594137600000,\"pizId\":\"f0a7927d-c5f9-4652-a053-1130d86c8fa8\",\"serviceId\":\"f0dbbdfe-3557-4428-ab1a-d2abad226524\",\"projectId\":\"7438817714\"}";

        JSONObject header = JSONUtil.parseObj(headerStr);
        JSONObject body = JSONUtil.parseObj(bodyStr);

        String appId = header.getStr("x-tsign-open-app-id");
        String signature = header.getStr("x-tsign-open-signature");
        String timestamp = header.getStr("x-tsign-open-timestamp");
        String algorithm = header.getStr("x-tsign-open-signature-algorithm");


        boolean verifySign = ApiUtil.verifySign(signature, timestamp, bodyStr, new EsignProperties().getAppSecret());
        System.out.println(verifySign);
    }

}
